/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ares.service.action;

import com.ares.service.bean.BeanCompetencia;
import com.ares.service.bean.BeanCualificaciones;
import com.ares.service.bean.BeanFormulario;
import com.ares.service.bean.BeanPilar;
import com.ares.service.util.Constantes;
import com.ares.service.util.Util;
import java.io.IOException;
import java.io.PrintWriter;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.lowagie.text.pdf.PdfCell;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.OutputStream;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.poi.hssf.util.HSSFColor.WHITE;

/**
 *
 * @author electro
 */
public class ImprimirCompetencias extends HttpServlet {
   //double calificacionGlobal = 3.54;
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */

    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            Document document = new Document(PageSize.A4);
            // step 2
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            // Comento
            PdfWriter.getInstance(document, baos);
            // step 3
            document.open();
            document.add(createTitulo("  GESTIÓN DE DESEMPEÑO", " EVALUACIÓN DE COMPETENCIAS"));
            document.add(createCabeceraCompetencias(request));
            try {
            	document.add(createIndicacionesCompetencias(request));
                document.add(createDetalleCompetencias(request));
                document.add(createEvaluacionFinalCompetencias(request));
            } catch (Exception ex) {
                Logger.getLogger(ImprimirCompetencias.class.getName()).log(Level.SEVERE, null, ex);
            }

            document.add(createCondiciones());
            // step 5
            document.close();

            // setting some response headers
            response.setHeader("Expires", "0");
            response.setHeader("Cache-Control",
                    "must-revalidate, post-check=0, pre-check=0");
            response.setHeader("Pragma", "public");
            // setting the content type
            response.setContentType("application/pdf");

            // the contentlength
            response.setContentLength(baos.size());
            OutputStream os = response.getOutputStream();
            baos.writeTo(os);
            os.flush();
            os.close();
        } catch (DocumentException ex) {
            throw new IOException(ex.getMessage());
        }
    }

    public PdfPTable createTitulo(String titulo, String dtTitulo) throws DocumentException {
        PdfPTable table = new PdfPTable(4);
        table.setWidths(new int[]{3, 7, 3, 7});
        table.setWidthPercentage(100);
        PdfPCell cell;
        Font font = new Font(CreateFont(), 10, Font.NORMAL, BaseColor.WHITE);
        cell = new PdfPCell(new Phrase(String.format(
                "%s", titulo), font));
        cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
        cell.setBackgroundColor(new BaseColor(0, 0, 0));
        cell.setColspan(4);
        cell.setFixedHeight(32f);
        table.addCell(cell);
        //
        cell = new PdfPCell(new Phrase(""));
        cell.setBackgroundColor(new BaseColor(178, 178, 176));
        cell.setBorder(PdfPCell.NO_BORDER);
        cell.setColspan(4);
        cell.setFixedHeight(4f);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase(""));
        cell.setBackgroundColor(new BaseColor(249, 184, 20));
        cell.setBorder(PdfPCell.NO_BORDER);
        cell.setColspan(4);
        cell.setFixedHeight(4f);
        table.addCell(cell);

        font = new Font(CreateFont(), 8, Font.NORMAL, BaseColor.WHITE);
        cell = new PdfPCell(new Phrase(String.format("%s", dtTitulo), font));
        cell.setBackgroundColor(new BaseColor(0, 0, 0));
        cell.setColspan(4);
        cell.setFixedHeight(16f);
        table.addCell(cell);

        return table;
    }

    public BaseFont CreateFont() {
        BaseFont arial = null;
        String fs = File.separator;
        String ruta = getServletContext().getRealPath(fs);
        
        try {
            arial = BaseFont.createFont(
                    ruta + "fuente" + fs + "arial.ttf", BaseFont.WINANSI, BaseFont.EMBEDDED);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return arial;
    }

    public PdfPTable createCabeceraCompetencias(HttpServletRequest request) throws DocumentException {
        HttpSession session = request.getSession();
        BeanFormulario formulario = (BeanFormulario) request.getAttribute("formulario");
        PdfPTable table = new PdfPTable(4);
        table.setWidths(new int[]{2, 7, 2, 7});
        table.setWidthPercentage(100);
        table.setHorizontalAlignment(PdfPTable.ALIGN_LEFT);

        PdfPCell cell;

        Font font = new Font(CreateFont(), 7, Font.NORMAL, BaseColor.BLACK);
        cell = new PdfPCell(new Phrase("Evaluado :", font));
        cell.setBorder(PdfPCell.NO_BORDER);
        table.addCell(cell);

        Font font2 = new Font(CreateFont(), 7, Font.NORMAL, BaseColor.BLACK);
        cell = new PdfPCell(new Phrase(formulario.getStrNom_Empleado()+" "+formulario.getStrApellido_Empleado(), font2));
        cell.setBorder(PdfPCell.NO_BORDER);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase("Evaluador :", font));
        cell.setBorder(PdfPCell.NO_BORDER);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase(formulario.getStrNom_Evaluador(), font2));
        cell.setBorder(PdfPCell.NO_BORDER);
        table.addCell(cell);
        
//SEGUNDA FILA
        cell = new PdfPCell(new Phrase("Puesto :", font));
        cell.setBorder(PdfPCell.NO_BORDER);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase(formulario.getStrPuesto_Empleado(), font2));
        cell.setBorder(PdfPCell.NO_BORDER);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase("Puesto :", font));
        cell.setBorder(PdfPCell.NO_BORDER);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase(formulario.getStrPuesto_Evaluador(), font2));
        cell.setBorder(PdfPCell.NO_BORDER);
        table.addCell(cell);

//TERCERA FILA
        cell = new PdfPCell(new Phrase("Área :", font));
        cell.setBorder(PdfPCell.NO_BORDER);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase(formulario.getStrArea_Empleado(), font2));
        cell.setBorder(PdfPCell.NO_BORDER);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase("", font));
        cell.setBorder(PdfPCell.NO_BORDER);
        cell.setColspan(2);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase(""));
        cell.setColspan(4);
        cell.setBorder(PdfPCell.NO_BORDER);
        cell.setFixedHeight(10f);
        table.addCell(cell);
        
        return table;
    }
    
    //DVQ 16112012
    public PdfPTable createIndicacionesCompetencias(HttpServletRequest request) throws DocumentException {
        PdfPTable table = new PdfPTable(1);
        table.setWidthPercentage(100);

        PdfPCell cell;

        Font fontNegrita = new Font(CreateFont(), 7, Font.BOLD, BaseColor.BLACK);
        cell = new PdfPCell(new Phrase("Competencias Genéricas", fontNegrita));
        cell.setBackgroundColor(new BaseColor(228, 228, 228));
        cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
        //cell.setBorder(PdfPCell.NO_BORDER);
        table.addCell(cell);

        Font font2 = new Font(CreateFont(), 7, Font.NORMAL, BaseColor.BLACK);
        cell = new PdfPCell(new Phrase(Constantes.COMPETENCIAS_GENERICAS, font2));
        //cell.setBorder(PdfPCell.NO_BORDER);
        cell.setHorizontalAlignment(PdfPCell.ALIGN_JUSTIFIED);
        table.addCell(cell);
        
        cell = new PdfPCell(new Phrase("", font2));
        cell.setFixedHeight(10f);
        cell.setBorder(PdfPCell.NO_BORDER);
        table.addCell(cell);
        
        return table;
    }
    
    public PdfPTable createDetalleCompetencias(HttpServletRequest request) throws DocumentException, Exception {
        //HttpSession session = request.getSession();
        List<BeanCompetencia> lstCompetencia = (List<BeanCompetencia>) request.getAttribute("lstCompetencia");
        //double promedioCalificacion = 0.00;
        PdfPTable table = new PdfPTable(4);

        table.setWidths(new int[]{8, 4, 4, 5});
        table.setWidthPercentage(100);
        
        int numeroDetallesXCompetencia = 0;
        boolean pintoJustificacion = false;
        double promedioFinalEvaluador = 0.0;
        double promedioFinalEvaluado = 0.0;
        double promedioEvaluador = 0.0;
        double promedioEvaluado = 0.0;
        int contadorCalificacionEvaluador = 0;
        int contadorCalificacionEvaluado = 0;
        
        Image imagen;
        String fs = File.separator;
        String ruta = getServletContext().getRealPath(fs);
        
        PdfPCell cell;

        Font fontcabeceraTabla = new Font(CreateFont(), 7, Font.BOLD, BaseColor.BLACK);
        cell = new PdfPCell(new Phrase("COMPETENCIA GENÉRICA", fontcabeceraTabla));
        cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);        
        cell.setBorder(PdfPCell.NO_BORDER);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase("EVALUACIÓN DEL JEFE", fontcabeceraTabla));
        cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
        cell.setBorder(PdfPCell.NO_BORDER);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase("AUTOEVALUACIÓN", fontcabeceraTabla));
        cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
        cell.setBorder(PdfPCell.NO_BORDER);
        table.addCell(cell);
        
        cell = new PdfPCell(new Phrase("JUSTIFICACIÓN AUTOEVALUACIÓN", fontcabeceraTabla));
        cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
        cell.setBorder(PdfPCell.NO_BORDER);
        table.addCell(cell);
        
        Font fontCabecera = new Font(CreateFont(), 7, Font.NORMAL, BaseColor.WHITE);
    	Font fontDetalle = new Font(CreateFont(), 7, Font.NORMAL, BaseColor.BLACK);
    	
        for(BeanCompetencia beanCompetencia : lstCompetencia) {
        	if(beanCompetencia.getTipo().equals("C")){
        		//numeroDetallesXCompetencia = beanCompetencia.getNumeroFilaDetalle();
        		pintoJustificacion = false;
        		
        		cell = new PdfPCell(new Phrase("" + beanCompetencia.getDescripcion(), fontCabecera));
                cell.setBackgroundColor(new BaseColor(0, 0, 0));
                cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
                table.addCell(cell);
                
                promedioEvaluador = devolverPromedioEvaluacion(lstCompetencia, beanCompetencia.getCodCabecera(), 1);
                promedioEvaluado = devolverPromedioEvaluacion(lstCompetencia, beanCompetencia.getCodCabecera(), 2);
                
                if(promedioEvaluador > 0){
                	promedioFinalEvaluador = promedioFinalEvaluador + promedioEvaluador;
                	contadorCalificacionEvaluador++;
                }
                
                if(promedioEvaluado > 0){
                	promedioFinalEvaluado = promedioFinalEvaluado + promedioEvaluado;
                	contadorCalificacionEvaluado++;
                }
                
                cell = new PdfPCell(new Phrase("" + convertirAString(promedioEvaluador), fontCabecera));
                cell.setBackgroundColor(new BaseColor(0, 0, 0));
                cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
                table.addCell(cell);
                
                cell = new PdfPCell(new Phrase("" + convertirAString(promedioEvaluado), fontCabecera));
                cell.setBackgroundColor(new BaseColor(0, 0, 0));
                cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
                table.addCell(cell);
                
                cell = new PdfPCell(new Phrase("", fontCabecera));
                cell.setBackgroundColor(new BaseColor(0, 0, 0));
                table.addCell(cell);
        	}
        	else if(beanCompetencia.getTipo().equals("D")){
        		numeroDetallesXCompetencia = beanCompetencia.getNumeroFilaDetalle();
        		
        		cell = new PdfPCell(new Phrase("" + beanCompetencia.getDescripcion(), fontDetalle));
    			cell.setHorizontalAlignment(PdfCell.ALIGN_JUSTIFIED);
                cell.setBackgroundColor(new BaseColor(255, 255, 255));
                table.addCell(cell);
                
                imagen = Image.getInstance(ruta + "images" + fs + devolverNombreImagen(beanCompetencia.getOpcionSeleccionado1()));
                imagen.scalePercent(50.0f);
                //PARA CENTRAR LA IMAGEN
                imagen.setAlignment(Image.ALIGN_MIDDLE);
                
                cell = new PdfPCell();
                cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
                cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
                cell.addElement(imagen);
                table.addCell(cell);
                
                imagen = Image.getInstance(ruta + "images" + fs + devolverNombreImagen(beanCompetencia.getOpcionSeleccionado2()));
                imagen.scalePercent(50.0f);
                //PARA CENTRAR LA IMAGEN
                imagen.setAlignment(Image.ALIGN_MIDDLE);
                
                cell = new PdfPCell();
                cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
                cell.setHorizontalAlignment(PdfPCell.ALIGN_MIDDLE);
                cell.addElement(imagen);
                table.addCell(cell);
                
                if(pintoJustificacion == false){
	                cell = new PdfPCell(new Phrase("" + validarTexto(beanCompetencia.getResultadoCadena1()), fontDetalle));
	    			cell.setHorizontalAlignment(PdfCell.ALIGN_JUSTIFIED);
	                cell.setBackgroundColor(new BaseColor(255, 255, 255));
	                cell.setRowspan(numeroDetallesXCompetencia);
	                table.addCell(cell);
	                //INICIALIZO LAS VARIABLES
	                numeroDetallesXCompetencia = 0;
	                pintoJustificacion = true;
                }
        	}
        }

        Font font3 = new Font(CreateFont(), 9, Font.BOLD, BaseColor.BLACK);
        cell = new PdfPCell(new Phrase("Puntuación Final ", font3));
        cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
        cell.setBorder(PdfPCell.NO_BORDER);
        table.addCell(cell);

        if(contadorCalificacionEvaluador > 0){
        	promedioFinalEvaluador = Redondear1Decimal(promedioFinalEvaluador/contadorCalificacionEvaluador);        	
        }
        
        if(contadorCalificacionEvaluado > 0){
        	promedioFinalEvaluado = Redondear1Decimal(promedioFinalEvaluado/contadorCalificacionEvaluado);
        }
        
        cell = new PdfPCell(new Phrase("" + promedioFinalEvaluador, font3));
        cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
        cell.setBorder(PdfPCell.NO_BORDER);
        table.addCell(cell);
        
        cell = new PdfPCell(new Phrase("" + promedioFinalEvaluado, font3));
        cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
        cell.setBorder(PdfPCell.NO_BORDER);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase("", font3));
        cell.setBorder(PdfPCell.NO_BORDER);
        table.addCell(cell);
        return table;
    }
    
    //DVQ 16112012
    public PdfPTable createEvaluacionFinalCompetencias(HttpServletRequest request) throws DocumentException {
    	List<BeanCompetencia> lstCompetencia = (List<BeanCompetencia>) request.getAttribute("lstCompetencia");
    	
    	PdfPTable table = new PdfPTable(1);
        table.setWidthPercentage(100);

        PdfPCell cell;

        Font fontBlanco = new Font(CreateFont(), 7, Font.NORMAL, BaseColor.WHITE);
        Font fontNegro = new Font(CreateFont(), 7, Font.NORMAL, BaseColor.BLACK);
        
        cell = new PdfPCell(new Phrase("", fontNegro));
        cell.setFixedHeight(10f);
        cell.setBorder(PdfPCell.NO_BORDER);
        table.addCell(cell);
                
        cell = new PdfPCell(new Phrase("Evaluación Final: Mencione a continuación las fortalezas y oportunidades de mejora de su colaborador," +
        		" expresando los comentarios que considere oportunos.", fontBlanco));
        cell.setBackgroundColor(new BaseColor(0, 0, 0));
        cell.setHorizontalAlignment(PdfPCell.ALIGN_JUSTIFIED);
        table.addCell(cell);
        
        cell = new PdfPCell(new Phrase("FORTALEZAS", fontNegro));
        cell.setBackgroundColor(new BaseColor(249, 184, 20));
        cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
        table.addCell(cell);
        
        //Falta lista de las 2 competencias     
        cell = new PdfPCell(new Phrase("1. " + devolverNombreCompetencia(lstCompetencia, request.getAttribute("codigoFortaleza1").toString()), fontNegro));
        cell.setBackgroundColor(new BaseColor(255, 255, 255));
        cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
        table.addCell(cell);
        
        cell = new PdfPCell(new Phrase(" " + validarTexto(request.getAttribute("forta1").toString()), fontNegro));
        cell.setBackgroundColor(new BaseColor(255, 255, 255));
        cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
        table.addCell(cell);
        
        cell = new PdfPCell(new Phrase("2. "  + devolverNombreCompetencia(lstCompetencia, request.getAttribute("codigoFortaleza2").toString()), fontNegro));
        cell.setBackgroundColor(new BaseColor(255, 255, 255));
        cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
        table.addCell(cell);
        
        cell = new PdfPCell(new Phrase(" "  + validarTexto(request.getAttribute("forta2").toString()), fontNegro));
        cell.setBackgroundColor(new BaseColor(255, 255, 255));
        cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
        table.addCell(cell);
        
        cell = new PdfPCell(new Phrase("Otras Fortalezas:", fontNegro));
        cell.setBackgroundColor(new BaseColor(255, 255, 255));
        cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
        table.addCell(cell);
        
        cell = new PdfPCell(new Phrase(" " + validarTexto(request.getAttribute("otrasFortalezas").toString()), fontNegro));
        cell.setBackgroundColor(new BaseColor(255, 255, 255));
        cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
        table.addCell(cell);
        
        cell = new PdfPCell(new Phrase("OPORTUNIDADES DE MEJORA", fontNegro));
        cell.setBackgroundColor(new BaseColor(249, 184, 20));
        cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
        table.addCell(cell);
        
        //Falta lista de las 2 competencias
        cell = new PdfPCell(new Phrase("1. " + devolverNombreCompetencia(lstCompetencia, request.getAttribute("codigoOportunidades1").toString()), fontNegro));
        cell.setBackgroundColor(new BaseColor(255, 255, 255));
        cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
        table.addCell(cell);
        
        cell = new PdfPCell(new Phrase(" " + validarTexto(request.getAttribute("oportunidad1").toString()), fontNegro));
        cell.setBackgroundColor(new BaseColor(255, 255, 255));
        cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
        table.addCell(cell);
        
        cell = new PdfPCell(new Phrase("2. " + devolverNombreCompetencia(lstCompetencia, request.getAttribute("codigoOportunidades2").toString()), fontNegro));
        cell.setBackgroundColor(new BaseColor(255, 255, 255));
        cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
        table.addCell(cell);
        
        cell = new PdfPCell(new Phrase(" " + validarTexto(request.getAttribute("oportunidad2").toString()), fontNegro));
        cell.setBackgroundColor(new BaseColor(255, 255, 255));
        cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
        table.addCell(cell);
        
        cell = new PdfPCell(new Phrase("Otras Oportunidades de Mejora:", fontNegro));
        cell.setBackgroundColor(new BaseColor(255, 255, 255));
        cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
        table.addCell(cell);
        
        cell = new PdfPCell(new Phrase(" " + validarTexto(request.getAttribute("otrasOportunidades").toString()), fontNegro));
        cell.setBackgroundColor(new BaseColor(255, 255, 255));
        cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
        table.addCell(cell);
        
        cell = new PdfPCell(new Phrase(" ", fontNegro));
        cell.setBackgroundColor(new BaseColor(255, 255, 255));
        cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
        cell.setBorder(PdfPCell.NO_BORDER);
        table.addCell(cell);
        
        cell = new PdfPCell(new Phrase("Comentarios del revisor.", fontBlanco));
        cell.setBackgroundColor(new BaseColor(0, 0, 0));
        cell.setHorizontalAlignment(PdfPCell.ALIGN_JUSTIFIED);
        table.addCell(cell);
        
        cell = new PdfPCell(new Phrase(" " + validarTexto(request.getAttribute("comentariosRevisor").toString()), fontNegro));
        cell.setBackgroundColor(new BaseColor(255, 255, 255));
        cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
        table.addCell(cell);
        
        return table;
    }

    public double Redondear2Decimales(double dblnum) {
        double numero = dblnum * 100;
        numero = Math.round(numero);
        dblnum = numero / 100;
        return dblnum;
    }
    
    public double Redondear1Decimal(double dblnum) {
        double numero = dblnum * 10;
        numero = Math.round(numero);
        dblnum = numero / 10;
        return dblnum;
    }

    public PdfPTable createCondiciones() throws DocumentException {
        PdfPTable table = new PdfPTable(2);
        table.setWidths(new int[]{1, 3});
        table.setWidthPercentage(35);
        table.setHorizontalAlignment(PdfPTable.ALIGN_LEFT);

        PdfPCell cell;
        Font font = new Font(CreateFont(), 8, Font.NORMAL, BaseColor.BLACK);
        
        cell = new PdfPCell(new Phrase(""));
        cell.setColspan(2);
        cell.setBorder(PdfPCell.NO_BORDER);
        cell.setFixedHeight(30f);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase("Firma del Colaborador:", font));
        cell.setColspan(2);
        cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
        cell.setFixedHeight(20f);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase("Firma del Jefe Directo:", font));
        cell.setColspan(2);
        cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
        cell.setFixedHeight(20f);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase("Firma del Revisor:", font));
        cell.setColspan(2);
        cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
        cell.setFixedHeight(20f);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase(""));
        cell.setColspan(2);
        cell.setBorder(PdfPCell.NO_BORDER);
        cell.setFixedHeight(30f);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase("Competencias evaluadas y discutidas el: __/__/__", font));
        cell.setBorder(PdfPCell.NO_BORDER);
        cell.setColspan(4);
        table.addCell(cell);

        return table;
    }

    private String devolverNombreImagen(String opcionSeleccionada){
		if(opcionSeleccionada != null){
			if(!opcionSeleccionada.equals("")){
				if(opcionSeleccionada.equals("0")){
					return "sinSeleccionar.jpg";
				}
				else{
					return "seleccionado" + opcionSeleccionada + ".jpg";
				}
			}
			else{
				return "sinSeleccionar.jpg";
			}
		}
		else{
			return "sinSeleccionar.jpg";
		}
	}
    
    private double devolverPromedioEvaluacion (List<BeanCompetencia> lstCompetencia, String idCabecera, int tipo){
    	int numeroDetalleXCabecera = 0;
    	double promedio = 0.0;
    	
    	if(!lstCompetencia.isEmpty()){
    		if(tipo == 1){
	    		for (BeanCompetencia beanCompetencia : lstCompetencia) {
					if(beanCompetencia.getTipo().equals("D")){
						if(beanCompetencia.getCodCabecera().equals(idCabecera)){
							if(beanCompetencia.getOpcionSeleccionado1() != null && beanCompetencia.getOpcionSeleccionado1() != "0"){
								promedio = promedio + Double.parseDouble(beanCompetencia.getOpcionSeleccionado1());
								numeroDetalleXCabecera ++;
							}
						}
					}
				}
	    		
	    		if(numeroDetalleXCabecera != 0){
	    			promedio = Redondear1Decimal(promedio/numeroDetalleXCabecera);
	    		}
    		}
    		else{
    			for (BeanCompetencia beanCompetencia : lstCompetencia) {
					if(beanCompetencia.getTipo().equals("D")){
						if(beanCompetencia.getCodCabecera().equals(idCabecera)){
							if(beanCompetencia.getOpcionSeleccionado2() != null && beanCompetencia.getOpcionSeleccionado2() != "0"){
								promedio = promedio + Double.parseDouble(beanCompetencia.getOpcionSeleccionado2());
								numeroDetalleXCabecera ++;
							}
						}
					}
				}
    			
    			if(numeroDetalleXCabecera != 0){
	    			promedio = Redondear1Decimal(promedio/numeroDetalleXCabecera);
	    		}
    		}
    	}
    	
    	return promedio;
    }

    private String devolverNombreCompetencia(List<BeanCompetencia> lstCompetencia, String idCabecera){
    	System.out.println("imprimir idCabecera: |" + idCabecera + "|");
    	if(!lstCompetencia.isEmpty()){
    		for (BeanCompetencia beanCompetencia : lstCompetencia) {
				if(beanCompetencia.getTipo().equals("C")){
					if(beanCompetencia.getCodCabecera().equals(idCabecera)){
						System.out.println("descripcion: |" + beanCompetencia.getDescripcion() + "|");
						return beanCompetencia.getDescripcion();
					}
				}
			}
    	}
    	
    	return "";
    }
    
    private String convertirAString(double promedio){
    	if(promedio > 0){
    		return String.valueOf(promedio);
    	}
    	else{
    		return "";
    	}
    }
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        System.out.println("Se ejecutó  el ImprimirCompetencias");
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    private String devuelveCalificacion(int calificacion) {
        String evaluacion = "";
        switch(calificacion){
            case 1:
                evaluacion = "1 - Demostró mínimamente";
                break;

            case 2:
                evaluacion = "2 - Demostró parcialmente";
                break;

            case 3:
                evaluacion = "3 - Demostró totalmente";
                break;

            case 4:
                evaluacion = "4 - Demostró de manera sobresaliente";
                break;

        }
        return evaluacion;
    }

    private String validarTexto(String justificacion) {
        return justificacion.replace("@", "\n");
    }

}
